<?php
namespace jy_creditshop;
defined('BY_JYA') or exit('error');
/*
	订单
*/
class order extends \plugin{
	//订单创建时，执行此方法，执行相关操作
	public $sendtypes=array(
		'1'=>'快递配送',
		'2'=>'自提/核销',
		'3'=>'虚拟发货',
	);
	public function create($params,$uid='',$url=''){
		$this->order=$params;
		$this->order['createtime']=TIMESTAMP;
		$uid=$uid?$uid:$_SESSION['uid'];
		$order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>intval($params['merchid'])?:$_SESSION['onemerchid'],
			'paymerchid'=>$params['onemerchid']?:$_SESSION['onemerchid'],
			'credit_type'=>$params['credit_type']?:1,
			'mid'=>$uid,
			'goodsid' => $params['goodsid'],
			'logno' =>'SN'.get_ordersn('jy_creditshop_log','logno'),
			'type' => $params['type'],
			'money' => $params['money']?$params['money']:$params['price'],
			'credit' => $params['credit'],
			'ratefee' => $params['ratefee'],
			'taocan' => ijson_encode($params['taocan']),
			'storeid' => $params['storeid'],
			'optionid' => $params['optionid'],
			'addressid' => $params['addressid'],
			'dispatch' => $params['dispatch'],
			'address' => $params['address'],
			'total' => $params['total'],
			'remark' => $params['remark'],
			'set_credit1' => intval($params['set_credit1'])*$params['total'],
			'status' => 0,
			'paystatus' => 0,
			'dispatchstatus' =>0,
			'createtime' => TIMESTAMP,
			'realname' => trim($params['realname']),
			'mobile' => trim($params['mobile'])
		);
		pdo_insert('jy_creditshop_log',$order);
		$order_id=$this->pdo_insertid();
		$this->order['id']=$order_id;
		$this->order['logno']=$order['logno'];
		$goods=$this->get('goods',array('id'=>$order['goodsid'],'uniacid'=>$_SESSION['uniacid']));
		//减去库存
		if($goods['totalcnf']==0){
			//有规格
			if($this->order['optionid']){
				pdo_update('jy_creditshop_option',array('total -='=>1),array('goodsid'=>$this->order['goodsid'],'id'=>$this->order['optionid']));
			}else{//无规格
				pdo_update('jy_creditshop_goods',array('total -='=>1),array('id'=>$this->order['goodsid'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		$url=custom_url('jy_creditshop/log_detail//id='.$order_id,true);
		$this->order['goods']=$goods;
		$this->model('notice')->notice_create2buyer($this->order,$url);
		//打印
		//$this->model('printmsg')->storeprint('',$order_id,0,0);
		return $order_id;
	}
	
	public function get_order($order_id,$merchid=0,$isup=false){
		if($this->order && $this->order['id']==$order_id && !$isup){
			return $this->order;
		}
		$this->order=$this->get('log',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$this->order['goods']=$this->get('goods',array('id'=>$this->order['goodsid'],'uniacid'=>$_SESSION['uniacid']));
		$this->order['goods']['goodsdetail']='';
		return $this->order;
	}
	
	//订单取消
	public function order_cancel($order_id,$istask=false){
		$this->get_order($order_id);
		if(!$this->order || $this->order['status'] !=0){
			if($istask){
				return false;
			}else{
				exi('订单不存在或者不支持取消！','error');
			}
		}
		$this->update('log',array('status'=>4),array('id'=>$order_id));
		$this->model('notice')->notice_cancel2buyer($this->order);
	}
	//订单删除
	public function order_delete($order_id,$userdeleted=false){
		$this->get_order($order_id);
		if(!$this->order){
			exi('订单不存在或者不支持删除！','error');
		}
		if($userdeleted){
			$this->update('log',array('userdeleted'=>1),array('id'=>$order_id));
		}else{
			$this->update('log',array('deleted'=>1),array('id'=>$order_id));
		}
	}
	public function order_rdelete($order_id){
		$this->get_order($order_id);
		if(!$this->order || $this->order['status']){
			exi('订单不存在或者不支持删除！','error');
		}
		pdo_delete('jy_creditshop_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
	}
	//订单恢复
	public function order_reset($order_id,$userdeleted=false){
		$this->get_order($order_id);
		if(!$this->order){
			exi('订单不存在！','error');
		}
		if($userdeleted){
			$this->update('log',array('userdeleted'=>0),array('id'=>$order_id));
		}else{
			$this->update('log',array('deleted'=>0),array('id'=>$order_id));
		}
	}
	public function order_cancelfh($order_id){
		$this->update('log',array('status'=>1),array('id'=>$order_id));
	}
	//支付
	public function order_pay($order_id,$params,$url=''){
		$this->get_order($order_id,'',true);
		if(!$this->order || $this->order['paystatus']){
			exi('订单不存在或者已支付！','error');
		}
		$credit_type=$this->order['credit_type']?:1;
		$update=array(
				'status'=>1,
				'paytype'=>$params['paytype'],
				'paytime'=>TIMESTAMP,
				'transid'=>$params['transid'],
				'credit_type'=>$credit_type,
				'paystatus'=>1,
		);
		$status=1;
		foreach($update as $key=>$value){
			$this->order[$key]=$value;
		}
		if($params['paytype']=='wechat' && $params['onemerchid']){
			$more['ismerchpay']=1;
		}
		$this->pdo_update('log',$update,array('id'=>$order_id));
		//打印
		//$this->model('printmsg')->storeprint('',$order_id,0,1);
		/*
			按商品虚拟商品和核销商品自动发货
		*/
		//已经整理出了商品类型type,isverify，只要支持核销，无论什么商品都可以核销！
		$credit_types=credit_types(true);
		if($this->order['goods']['isverify']==1){
				$this->order_send_verify($order_id,$this->order['goodsid']);
				$status=2;
		}
		//支付成功，减去用户对应的积分
		
		$credit_title=$credit_types[$credit_type];
		$fee=$this->order['credit']+$this->order['ratefee'];
		if($this->order['paymerchid']){
			model('jy_merch/member')->member_credit($this->order['paymerchid'],$this->order['mid'],'credit'.$credit_type,$fee,0,'jy_creditshop',$credit_title.'兑换');
		}else{
			cfc('core')->member_credit($this->order['mid'],'credit'.$credit_type,$fee,0,'jy_creditshop',$credit_title.'兑换');
		}
		if($_SESSION['nowcredit1']){
			$_SESSION['nowcredit1']=$_SESSION['nowcredit1']-$fee;
		}
		$iszhong=true;
		if($this->order['goods']['type']==1){
			$iszhong=$this->choujiang($this->order);
		}
		//奖励积分
		if($this->order['set_credit1']>0){
			$setcredit_title=$credit_types[$this->order['set_credit1']];
			if($this->order['paymerchid']){
				model('jy_merch/member')->member_credit($this->order['paymerchid'],$this->order['mid'],'credit1',$this->order['set_credit1'],1,'jy_creditshop','参与商品兑换，奖励'.$setcredit_title,'','',$more);
			}else{
				cfc('core')->member_credit($this->order['mid'],'credit1',$this->order['set_credit1'],1,'jy_creditshop','参与商品兑换，奖励'.$setcredit_title);
			}
		}
		if(!$iszhong){
			//修改日志支付状态
			pdo_update('jy_creditshop_log',array('paystatus'=>1,'status'=>5),array('id'=>$this->order['id']));
			$this->model('notice')->notice_pay2buyer($this->order,$url);
			return false;
		}
		//减去库存
		if($this->order['goods']['totalcnf']==1){
			//有规格
			if($this->order['optionid']){
				pdo_update('jy_creditshop_option',array('total -='=>1),array('goodsid'=>$this->order['goodsid'],'id'=>$this->order['optionid']));
			}else{//无规格
				pdo_update('jy_creditshop_goods',array('total -='=>1),array('id'=>$this->order['goodsid'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		pdo_update('jy_creditshop_goods',array('salenum +='=>1),array('id'=>$this->order['goodsid'],'uniacid'=>$_SESSION['uniacid']));
		$goods=$this->order['goods'];
		//商品类型
		if($goods['goodstype']==0){
			
		}elseif($goods['goodstype']==1){//优惠券
				$params=array(
					'mid'=>$this->order['mid'],
					'ac_type'=>'goods',
					'ac_id'=>$goods['couponid'],
					'remark'=>'积分兑换优惠券',
				);
				model('jy_weishop/coupon')->give_coupon($params);
				$status=3;
		}elseif($goods['goodstype']==2){//余额
				if($this->order['paymerchid']){
					model('jy_merch/member')->member_credit($this->order['paymerchid'],$this->order['mid'],'credit2',$goods['grant1']*$this->order['total'],1,'jy_creditshop',$credit_title.'兑换余额','','',$more);
				}else{
					cfc('core')->member_credit($this->order['mid'],'credit2',$goods['grant1']*$this->order['total'],1,'jy_creditshop',$credit_title.'兑换余额');
				}
				$status=3;
		}elseif($goods['goodstype']==3){//红包
				/*if($goods['packettype']==1){
					$money=$goods['grant2'];
				}else{
					$money=rand($goods['minpacketmoney']*100,$goods['maxpacketmoney']*100)/100;//金额
				}
				$params=array(
					'money'=>$money,
					'remark'=>'积分兑换红包',
				);
				model('core/member')->payred_out($this->order['mid'],$params);*/
				$status=2;
		}elseif($goods['goodstype']>=4){//虚拟币
				if($this->order['paymerchid']){
					model('jy_merch/member')->member_credit($this->order['paymerchid'],$this->order['mid'],'credit'.$goods['goodstype'],$goods['grant1']*$this->order['total'],1,'jy_creditshop',$credit_title.'兑换获得','','',$more);
				}else{
					cfc('core')->member_credit($this->order['mid'],'credit'.$goods['goodstype'],$goods['grant1']*$this->order['total'],1,'jy_creditshop',$credit_title.'兑换获得');
				}
				$status=3;
		}
		//修改日志支付状态
		pdo_update('jy_creditshop_log',array('paystatus'=>1,'status'=>$status),array('id'=>$this->order['id']));
		$this->model('notice')->notice_pay2buyer($this->order);
		if($goods['noticemid']){
			$noticemid=json_decode($goods['noticemid'],true);
			foreach($noticemid as $nv){
				if($nv['mid']){
					$this->model('notice')->notice_pay2seller($this->order,$nv['mid']);
				}
			}
		}
	}
	//抽奖
	public function choujiang($order){		
		$key=mt_rand(1,100);
		if($key<=$goods['rate']){
			return true;
		}else{
			return false;
		}
	}
	//发货
	public function order_send($order_id,$params=array()){
		$update=array(
			'status'=>2,
			'time_send'=>time(),
			'express'=>intval($params['express']),
			'expresssn'=>$params['expresssn'],
		);
		$this->update('log',$update,array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$this->get_order($order_id);
		$this->order['sendtime']=TIMESTAMP;
		$notice_setting=$this->plugin_setting('notice');
		$this->model('notice')->notice_send2buyer($this->order);
	}
	//自动发货
	public function order_vsend($order_id,$params=array()){
		$this->update('order',array('status'=>2,'sendtime'=>TIMESTAMP,'finishtime'=>TIMESTAMP),array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$this->get_order($order_id);
		$this->order['sendtime']=TIMESTAMP;
		$this->order['finishtime']=TIMESTAMP;
		$this->order['mobile']=pdo_getcolumn('core_members',array('id'=>$this->order['mid']),'mobile');	
		$this->order['nickname']=$nickname=pdo_getcolumn('core_members',array('id'=>$this->order['mid']),'nickname');
		$this->order_receive($order_id,true);
		$notice_setting=$this->plugin_setting('notice');
		$this->model('notice')->notice_vsend2buyer($this->order);
		if($notice_setting['seller']){
			foreach($notice_setting['seller'] as $member){
				$this->model('notice')->notice_send2seller($this->order,$member['mid']);
			}
		}
		
	}
	public function order_send_virtual($order_id,$goods=false){
		if(is_string($goods)){
			//兼容直接自定义发货内容的类型
			$content=$goods;
			$goods=false;
		}
		if(!$goods){
			$goodsid=$this->pdo_getcolumn('order_goods',array('orderid'=>$orderid),'goodsid');
			$goods=$this->pdo_get('goods',array('id'=>$goodsid),array('id','type','virtualsend','virtualsendcont','virtual_template_id'));
			if($content){
				$goods['virtualsendcont']=$content;
			}
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'orderid'=>$order_id,
			'goodsid'=>$goods['goodsid'],
			'createtime'=>TIMESTAMP,
		);
		$virtualinfo='';
		for($i=0;$i<$goods['total'];$i++){
				$num=$i+1;
				$data['content']='';
				if($goods['type']==1){
					//发需虚拟物品
					for($i=0;$i<$goods['total'];$i++){
						$num=$i+1;
						$data['content'] .='发货信息('.$num.'):'.$goods['virtualsendcont'];
					}
				}else{
				//发卡密
					$virtual_data=$this->pdo_get('virtual_data',array('tempid'=>$goods['virtual_template_id'],'mid'=>0),array('id','fields'));
					$virtual_update=array(
						'mid'=>$this->order['mid'],
						'usetime'=>TIMESTAMP,
						'orderid'=>$this->order['id'],
						'ordersn'=>$this->order['ordersn'],
						'price'=>$this->order['price'],
					);
					$this->pdo_update('virtual_data',$virtual_update,array('id'=>$virtual_data['id']));
					//更新卡密库存
					$this->model('order')->up_virtualtotal($goods['virtual_template_id']);
					$virtual_temp=$this->pdo_get('virtual_temp',array('id'=>$goods['virtual_template_id']));
					if($virtual_data && $virtual_temp){
						$virtual_data['fields']=json_decode($virtual_data['fields'],true);
						$virtual_temp['fields']=json_decode($virtual_temp['fields'],true);
						foreach($virtual_temp['fields'] as $field){
							$data['content'].=$field['title'].'('.$num.')：'.$virtual_data['fields'][$field['kw']].' ';
						}
					}
					$data['virtualid']=$goods['virtual_template_id'];
				}
				$virtualinfo.=$data['content'];
				$data['merchid']=$_SESSION['onemerchid'];
				$this->pdo_insert('order_virtual',$data);
		}
		$this->order['virtualinfo']=$virtualinfo;
		$this->pdo_update('order',array('virtualsendinfo'=>$virtualinfo),array('id'=>$this->order['id']));
		//更新订单状态，发货
		$this->order_vsend($order_id);
		return true;
	}
	public function order_send_verify($order_id,$goodsid='',$sendtype=0){
		//自提核销
		$goods=$this->pdo_get('goods',array('id'=>$goodsid),array('verifytype','usetime'));
		if($goods['endtime']){
			$upusetime=$goods['endtime'];
		}else{
			$upusetime=$goods['usetime']?TIMESTAMP+$goods['usetime']*24*3600:0;
		}
		$order_verify=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'logid'=>$order_id,
			'endtime'=>$upusetime,
			'upusetime'=>$upusetime,
			'createtime'=>TIMESTAMP,
			'isverify'=>0,
			'verifycode'=>random(10),
		);
		if($goods['verifytype']==0){
				//按订单核销
				do{
					$verify_code=random(10);
				}while($this->pdo_getcolumn('verify',array('uniacid'=>$_SESSION['uniacid'],'verifycode'=>$verify_code),'id'));
				$data=array(
					'verifytype'=>0,
					'verifycode'=>$verify_code,
				);
				$order_verify=array_merge($data,$order_verify);
				$order_verify['merchid']=$_SESSION['onemerchid'];
				$this->pdo_insert('verify',$order_verify);
		}elseif($goods['verifytype']==1){
				//按次核销
				do{
					$verify_code=random(10);
				}while($this->pdo_getcolumn('verify',array('uniacid'=>$_SESSION['uniacid'],'verifycode'=>$verify_code),'id'));
				
				$data=array(
					'verifytype'=>1,
					'verifynum'=>$goods['verifynum'],
					'verifycode'=>$verify_code,
				);
				$order_verify=array_merge($data,$order_verify);
				$order_verify['merchid']=$_SESSION['onemerchid'];
				$this->pdo_insert('verify',$order_verify);
		}
		$this->update('log',array('status'=>2),array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function order_verify($verify_code,$clerk_id,$noexi=false){
		$order_verify=$this->pdo_get('verify',array('verifycode'=>$verify_code));
		if(!$order_verify){
			if($noexi){
					return true;
			}
			exi('核销码不存在！','error');
		}
		if($order_verify['isverify'] ){
			if($noexi){
					return true;
			}
			exi('核销码已被使用或已过期！','error');
		}
		if($order_verify['endtime'] && $order_verify['endtime']<TIMESTAMP){
			$this->pdo_update('verify',array('isverify'=>2),array('logid'=>$order_verify['logid']));
			if($noexi){
					return true;
			}
			exi('该核销码已过期！','error');
		}
		if($order_verify['verifytype']==0){
			//按订单核销
			$update=array(
				'isverify'=>1,
				'clerk_id'=>$clerk_id,
				'verifytime'=>TIMESTAMP,
			);
			$this->pdo_update('verify',$update,array('id'=>$order_verify['id']));
			$this->order_receive($order_verify['logid']);
		}elseif($order_verify['verifytype']==1){
			//按次核销
			if($order_verify['verifynum']<=0){
				$this->pdo_update('verify',array('isverify'=>1),array('id'=>$order_verify['id']));
				if($noexi){
					return true;
				}
				exi('核销次数已用完！','error');
			}
			$verifydata=$order_verify['verifydata']?json_decode($order_verify['verifydata'],true):array();
			$verifydata[]=array(
				'clerk_id'=>$clerk_id,
				'verifytime'=>TIMESTAMP,
			);
			$update=array(
				'verifydata'=>ijson_encode($verifydata),
				'verifynum -='=>1,
			);
			if($order_verify['verifynum']==1){
				$update['isverify']=1;
			}
			$this->pdo_update('verify',$update,array('id'=>$order_verify['id']));
			if($update['isverify']==1){
				$this->order_receive($order_verify['logid']);
			}
		}
	}
	//确认收货，完成订单
	public function order_receive($order_id,$ismsg=false){
		$update=array('status'=>3,'time_finish'=>TIMESTAMP);
		$this->update('log',$update,array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$this->get_order($order_id);
		$this->model('commission')->order_complete($order_id);
		$this->model('notice')->notice_receive2buyer($this->order);
	}
	public function sendsubmsg($order_id){
		$this->get_order($order_id);
		$this->model('notice')->notice_pay2buyer($this->order);
		return true;
	}
	public function check_times($goods){
		$istime=pdo_count('jy_creditshop_log',array('status'=>array(1,2,3,5),'goodsid'=>$goods['id'],'mid'=>$_SESSION['uid']));
		if($goods['chance'] && $istime>=$goods['chance']){
			exi('最多兑换'.$goods['chance'].'次,您已兑换了'.$istime.'次！不能再兑换了！','error','goback');
		}
		return $istime;
	}
	public function check_buy($goods,$isexi=false){
		$goods['canbuy'] = !(empty($goods['status'])) && empty($goods['deleted'])?1:2;
		if ($goods['canbuy']==2) {		
			$goods['buymsg'] = '已下架';
		}else{	
			if($goods['goodstype'] == 3){
				if (($goods['packetsurplus'] <= 0) || ($goods['surplusmoney'] <= $goods['packetlimit'])) 
				{
					$goods['canbuy'] = 2;
					$goods['buymsg'] = ((empty($goods['type']) ? '已兑完' : '已抽完'));
				}
			}else if($goods['goodstype'] > 3){
				
			}else if (0 < $goods['total']){
				$logcount = pdo_fetchcolumn('select count(*) from ' . tablename('jy_creditshop_log') . '  where goodsid=:goodsid and status>=2  and uniacid=:uniacid  ', array(':goodsid' => $id, ':uniacid' => $_SESSION['uniacid']));
				$goods['logcount'] = $logcount;
				if ($goods['joins'] < $logcount){
					pdo_update('jy_creditshop_goods', array('joins' => $logcount), array('id' => $id));
				}
				if($goods['istime']){
					$weekid=date("w");
					$weekid=$weekid?:7;
					$iswork=strpos($goods['work_time'],';'.$weekid.';');
					if($goods['timestart']>time()){
						$goods['canbuy'] = 2;
						$goods['buymsg']='还未开始兑换';
					}elseif($goods['timeend']<time()){
						$goods['canbuy'] = 2;
						$goods['buymsg']='兑换已结束';
					}elseif($iswork === false){
						$goods['canbuy'] = 2;
						$goods['buymsg']='今天不支持兑换';
					}
				}
			}else{
				$goods['canbuy'] = 2;
				$goods['buymsg'] = ((empty($goods['type']) ? '已兑完' : '已抽完'));
			}
		}
		$res=array(
			'canbuy'=>$goods['canbuy'],
			'buymsg'=>$goods['buymsg'],
		);
		if($isexi && $goods['canbuy']==2){
			exi($goods['buymsg'],'error');
		}else{
			return $res;
		}
	}
	//自动核销
	public function up_verify(){
		$list=pdo_getall('jy_creditshop_verify',array('isverify'=>0,'endtime <'=>TIMESTAMP,'endtime >'=>100));
		foreach($list as $item){
			if($item['endtime']){
				$this->model('order')->order_verify($item['verifycode'],$item['mid'],true);
			}
		}
		return $istime;
	}
	//退款
	public function rafund($id=0){
		$order=pdo_get('jy_creditshop_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		if($order['status'] !=1){
			exi('订单状态不支持取消！','error');
		}
		if($order['money']>0){
			$pay_out_id=$this->refund_pay($order);
		}
		$credit_types=credit_types(true);
		$credit_title=$credit_types[$order['credit_type']];
		$credit=$order['credit']+$order['ratefee'];
		if($credit>0){
			if($order['paytype']=='wechat' && $order['paymerchid']){
				$more['ismerchpay']=1;
			}
			if($this->setting['istui_fee'] && strpos($this->setting['tui_feetype'],';'.$order['credit_type'].';') !== false){
				if($order['paymerchid']){
					model('jy_merch/member')->member_credit($order['paymerchid'],$order['mid'],'credit2',$credit,1,'jy_creditshop',$credit_title.'退币到余额','','',$more);
				}else{
					cfc('core')->member_credit($order['mid'],'credit2',$credit,1,'jy_creditshop',$credit_title.'退币到余额');
				}
			}else{
				if($order['paymerchid']){
					model('jy_merch/member')->member_credit($order['paymerchid'],$order['mid'],'credit'.$order['credit_type'],$credit,1,'jy_creditshop',$credit_title.'退币','','',$more);
				}else{
					cfc('core')->member_credit($order['mid'],'credit'.$order['credit_type'],$credit,1,'jy_creditshop',$credit_title.'退币');
				}
			}
		}
		$update=array('status'=>6,'time_finish'=>TIMESTAMP,'pay_out_id'=>$pay_out_id);
		pdo_update('jy_creditshop_log',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		return $istime;
	}
	//退款支付
	public function refund_pay($order=array(),$noexi=false){
		if($order['paytype']=='credit' || $order['paytype']=='apipay'){
			$pay_out_id=model('core/member')->add_credit2($order['mid'],$order['money'],'订单退款-'.$order['id'],'jy_creditshop');
		}elseif(strpos($order['paytype'],'credit') !== false){
			$pay_out_id=model('core/member')->member_credit($order['mid'],$order['paytype'],$order['money'],true,'','订单退款');
		}elseif($order['paytype']=='wechat'){
			$payinfo=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pay_id']));
			$transaction_id=$order['transid']?:$payinfo['transaction_id'];
			if(!$transaction_id){
				if($noexi){
					return false;
				}
				exi('退款失败，没有微信支付号！','error');
			}
			$payinfo['transid']=$transaction_id;
			$payinfo['money']=$order['money'];
			$payinfo['remark']='订单退款';
			$pay_out_id=model('core/member')->refund_out($order['mid'],$payinfo,$noexi);
		}elseif($order['paytype']=='alipay'){
			$payinfo=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pay_id']));
			$transaction_id=$order['transid']?:$payinfo['transaction_id'];
			if(!$transaction_id){
				if($noexi){
					return false;
				}
				exi('退款失败，没有支付宝支付号！','error');
			}
			$params=array(
				'money'=>$order['money'],
				'ordersn'=>$order['logno'],
				'transid'=>$transaction_id,
				'remark'=>'订单退款',
			);
			$pay_out_id=model('core/member')->refund_out($order['mid'],$params,$noexi,'alipay');
		}else{
			$pay_out_id=model('core/member')->add_credit2($order['mid'],$order['money'],'订单退款-'.$order['id'],'jy_creditshop');
		}
		return $pay_out_id;
	}
}